﻿package  {
	import flash.display.MovieClip;

	public class Grid extends MovieClip{

		private var _rooms:Array; //Array of Arrays of Rooms
		private var _startRoom:Room;
		private var _currentRoom:Room;
		private var _chestCount:int=0;
		private var _switchCount:int=0;
		
		public function Grid() {
			// constructor code
			_rooms = new Array();
			for(var i:int = 0; i < 5; i++)
			{
				_rooms[i] = new Array();
			}
		}
		
		public function get ChestCount():int{return _chestCount;}
		public function get SwitchCount():int{return _switchCount;}
		public function get startRoom():Room{return _startRoom;}
		public function get rooms():Array{return _rooms;}
		
		public function set ChestCount(a:int){_chestCount = a;}
		public function set SwitchCount(a:int){_switchCount = a;}
		
		
		
		public function CreateRooms(stageWidth:int,stageHeight:int, p:Player)
		{
			
			var numRooms:uint = GameDataStore.sharedInstance.xml.room.length();
			//trace("" + numRooms);
			var gameXML:XML = GameDataStore.sharedInstance.xml;
			for(var i:int = 0; i < numRooms; i++){
				var locX:uint = gameXML.room[i].@X;
				var locY:uint = gameXML.room[i].@Y;
				var cheese:uint = gameXML.room[i].@id;
				//trace("HMM");
				_rooms[locX][locY] = new Room(i,stageWidth,stageHeight,locX,locY,p,this);
				//trace("MADE ROOM" + i);
				if(cheese == 0){
					_startRoom = _rooms[locX][locY];
					_currentRoom = _startRoom;
				}
			}
		}
		
		public function ChangeRoom(currentX:int,currentY:int,modX:int,modY:int,him:Player){
			parent.removeChild(_rooms[currentX][currentY]);
			(_rooms[currentX+modX][currentY+modY] as Room).player = him;
			_currentRoom = _rooms[currentX+modX][currentY+modY];
			parent.addChild(_rooms[currentX+modX][currentY+modY]);
		}
		
		public function Update(){
			_currentRoom.Update();
		}

	}
	
}
